package com.lineage.data.item_etcitem;

import com.lineage.DatabaseFactory;
import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.NpcTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1MonsterInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Object;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.S_WhoCharinfo;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Npc;
import com.lineage.server.utils.SQLUtil;
import com.lineage.server.world.World;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Pc_Npc_show extends ItemExecutor {
  public static ItemExecutor get() {
    return new Pc_Npc_show();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance useItem) {
    int spellsc_objid = data[0];
    L1Object target = World.get().findObject(spellsc_objid);
    if (target != null) {
      int i = 0, j = 0;
      String msg0 = "";
      String msg2 = "";
      String msg3 = "";
      String msg4 = "";
      String msg5 = "";
      String msg6 = "";
      String msg7 = "";
      String msg8 = "";
      String msg9 = "";
      String msg10 = "";
      String msg11 = "";
      String msg12 = "";
      String msg13 = "";
      String msg14 = "";
      String msg15 = "";
      String msg16 = "";
      String msg17 = "";
      String msg18 = "";
      String msg19 = "";
      String msg20 = "";
      if (target instanceof L1PcInstance) {
        L1PcInstance target_pc = (L1PcInstance)target;
        int mr = 0;
        switch (target_pc.guardianEncounter()) {
          case 0:
            mr = 3;
            break;
          case 1:
            mr = 6;
            break;
          case 2:
            mr = 9;
            break;
        } 
        msg0 = target_pc.getName();
        int k = target_pc.getLevel();
        msg3 = String.valueOf(target_pc.getCurrentHp()) + " / " + target_pc.getMaxHp();
        msg4 = String.valueOf(target_pc.getCurrentMp()) + " / " + target_pc.getMaxMp();
        int m = target_pc.getAc();
        short s1 = target_pc.getStr();
        short s2 = target_pc.getDex();
        short s3 = target_pc.getInt();
        short s4 = target_pc.getCon();
        short s5 = target_pc.getWis();
        short s6 = target_pc.getCha();
        msg12 = String.valueOf(target_pc.getMr() + mr) + " %";
        i = target_pc.getLawful();
        j = target_pc.getSp();
        S_WhoCharinfo whoChar = new S_WhoCharinfo(pc, target_pc);
        pc.sendPackets((ServerBasePacket)whoChar);
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fT等级:[" + k + "]"));
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fW血量:[" + msg3 + "] // 魔力:[" + msg4 + "]"));
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fU防御:[" + m + "]  //  魔防:[" + msg12 + "]"));
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fU正义:[" + i + "]  //  魔攻:[" + j + "]"));
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fR力量:[" + s1 + "]  //  敏捷:[" + s2 + "]"));
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fR智力:[" + s3 + "]  //  体质:[" + s4 + "]"));
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fR精神:[" + s5 + "]  //  魅力:[" + s6 + "]"));
      } else if (target instanceof L1MonsterInstance) {
        L1MonsterInstance target_npc = (L1MonsterInstance)target;
        msg0 = target_npc.getName();
        int k = target_npc.getLevel();
        msg3 = String.valueOf(target_npc.getCurrentHp()) + " / " + target_npc.getMaxHp();
        msg4 = String.valueOf(target_npc.getCurrentMp()) + " / " + target_npc.getMaxMp();
        int m = target_npc.getAc();
        msg6 = "0";
        msg7 = String.valueOf(target_npc.getMr()) + " %";
        msg8 = String.valueOf(target_npc.getFire()) + " %";
        msg9 = String.valueOf(target_npc.getWater()) + " %";
        msg10 = String.valueOf(target_npc.getWind()) + " %";
        msg11 = String.valueOf(target_npc.getEarth()) + " %";
        msg12 = "0";
        short s1 = target_npc.getStr();
        short s2 = target_npc.getInt();
        short s3 = target_npc.getCon();
        short s4 = target_npc.getDex();
        short s5 = target_npc.getWis();
        long l = target_npc.getExp();
        String[] msg21 = { 
            msg0, ""+k, msg3, msg4, ""+m, msg6, msg7, msg8, msg9, msg10, 
            msg11, msg12, 
            ""+s1, ""+s2, ""+s3, ""+s4, ""+s5, ""+l, ""+i, ""+j };
        pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(pc.getId(), "ajplayer1", msg21));
        L1Npc npc = NpcTable.get().getTemplate(target_npc.getNpcId());
        if (npc == null) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\aT不存在该怪物。你可以在怪物附近输入.4查看id"));
          return;
        } 
        Connection con = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
          con = DatabaseFactory.get().getConnection();
          pstm = con.prepareStatement("SELECT itemId,min,max,chance FROM droplist WHERE mobId=?");
          pstm.setInt(1, target_npc.getNpcId());
          rs = pstm.executeQuery();
          rs.last();
          int rows = rs.getRow();
          int[] itemID = new int[rows];
          int[] min = new int[rows];
          int[] max = new int[rows];
          double[] chance = new double[rows];
          String[] name = new String[rows];
          rs.beforeFirst();
          int n = 0;
          while (rs.next()) {
            itemID[n] = rs.getInt("itemId");
            min[n] = rs.getInt("min");
            max[n] = rs.getInt("max");
            chance[n] = rs.getInt("chance") / 10000.0D;
            n++;
          } 
          rs.close();
          pstm.close();
          if (pc.isGm()) {
            pc.sendPackets((ServerBasePacket)new S_SystemMessage(
                  "\\fR" + npc.get_name() + "(" + target_npc.getNpcId() + ") 常规掉落查询:"));
          } else {
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fR(" + npc.get_name() + ") 常规掉落查询:"));
          } 
          int i1 = 0;
          while (i1 < itemID.length) {
            pstm = con.prepareStatement("SELECT name FROM etcitem WHERE item_id=?");
            pstm.setInt(1, itemID[i1]);
            rs = pstm.executeQuery();
            while (rs.next())
              name[i1] = rs.getString("name"); 
            rs.close();
            pstm.close();
            pstm = con.prepareStatement("SELECT name FROM weapon WHERE item_id=?");
            pstm.setInt(1, itemID[i1]);
            rs = pstm.executeQuery();
            while (rs.next())
              name[i1] = rs.getString("name"); 
            rs.close();
            pstm.close();
            pstm = con.prepareStatement("SELECT name FROM armor WHERE item_id=?");
            pstm.setInt(1, itemID[i1]);
            rs = pstm.executeQuery();
            while (rs.next())
              name[i1] = rs.getString("name"); 
            rs.close();
            pstm.close();
            if (pc.isGm()) {
              pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fY物品: " + name[i1] + " " + " 几率:" + chance[i1] + "%"));
            } else {
              pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fY物品: " + name[i1]));
            } 
            i1++;
          } 
        } catch (Exception exception) {
        
        } finally {
          SQLUtil.close(rs);
          SQLUtil.close(pstm);
          SQLUtil.close(con);
        } 
      } 
    } 
  }
}
